//
//  CollectionViewController.m
//  XhlTrip
//
//  Created by dllo on 15/8/31.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "CollectionViewController.h"
#import "DataBase.h"
#import "TravelViewCell.h"
#import "TravelDetailController.h"
#import "DBModel.h"
#import "TravelModel.h"

@interface CollectionViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableview;
@property (nonatomic, retain) NSMutableArray *collectArr;

@end

@implementation CollectionViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.collectArr = [NSMutableArray array];
    }
    return self;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTableView];
    self.navigationController.automaticallyAdjustsScrollViewInsets = NO;
}


- (void)createTableView
{
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 5 * Height, self.view.frame.size.width, self.view.frame.size.height - 69 * Height) style:UITableViewStylePlain];
    self.tableview.dataSource = self;
    self.tableview.delegate = self;
     _tableview.rowHeight = 210 * Height;
    [self.view addSubview:self.tableview];
    [_tableview release];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.collectArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TravelViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tt"];
    if (cell == nil) {
        cell = [[[TravelViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"tt"] autorelease];
        
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    DBModel *dabmodel = [[[DBModel alloc] init] autorelease];
    dabmodel = self.collectArr[indexPath.row];
    TravelModel *travel = [[[TravelModel alloc] init] autorelease];
    travel.name = dabmodel.name;
    travel.photos_count = dabmodel.photos_count;
    travel.start_date = dabmodel.start_date;
    travel.days = dabmodel.days;
    travel.front_cover_photo_url = dabmodel.front_cover_photo_url;
    cell.SmallImageView.hidden = YES;
    cell.daysLabel.hidden = YES;
    cell.travelModel = travel;
    return cell;    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TravelDetailController *travelDetail = [[TravelDetailController alloc] init];
    DBModel *model = [[DBModel alloc] init];
    model = self.collectArr[indexPath.row];
   
    [travelDetail passDetailValue:[NSString stringWithFormat:@"%@", model.number]];
   
    [self.navigationController pushViewController:travelDetail animated:YES];
    [travelDetail release];

}


- (void)dealloc
{
    [_collectArr release];
    [_tableview release];
    [super dealloc];
}


- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    
    /* 清除数据要不一反回就重复 */
    [self.collectArr removeAllObjects];
    [self.collectArr addObjectsFromArray:[NSMutableArray arrayWithArray:[DataBase selectall]]];
    NSLog(@" 收藏的数组元素个数 %ld", self.collectArr.count);
    if (self.collectArr.count == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"您还没有收藏喜欢的游记" message:@"快去浏览好看的游记吧" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
        [alert release];
    }
    [_tableview reloadData];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
